package com.laicunba.ext.web.controller.h5.activity;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.laicunba.ext.service.SlaveVcoinService;
import com.laicunba.ext.web.controller.SuiCunBaoBaseController;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.RegistryService;
import com.laicunba.service.VcoinWalletService;
 
//@Controller
@RequestMapping(value = "/h5/ext/activity")
public class DragonBoatController extends SuiCunBaoBaseController {
	private static final Logger LOG = Logger.getLogger(DragonBoatController.class);
	@Autowired
	private RegistryService registryService;
	@Autowired
	private VcoinWalletService vcoinWalletService;
	@Autowired
	private SlaveVcoinService slaveVcoinService;
	
	@RequestMapping(value = "/dragon_boat_festival.htm")
	public ModelAndView dragonBoatFestival(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dragon_boat_festival.jsp");
		UserDO loginUser = getCurrentUser(request);
		if(loginUser != null) {
			//清理老活动的累积积分，如果已清理则不再次进行
			RegistryDO cleanInviteVcoinRegistry = registryService.get(RegistryDO.class, "u." + loginUser.getId() + ".clean-dragon-boat-vcoin");
			if(cleanInviteVcoinRegistry == null) {
				long balance = vcoinWalletService.getBalance(loginUser.getId());
				if(balance > 0) {
					vcoinWalletService.subtract(loginUser.getId(), (int) balance, "DRAGON_BOAT_FESTIVAL_CLEAN", "端午节活动积分清零");
				}
				
				cleanInviteVcoinRegistry = new RegistryDO();
				cleanInviteVcoinRegistry.setId("u." + loginUser.getId() + ".clean-dragon-boat-vcoin");
				cleanInviteVcoinRegistry.setVal(String.valueOf(balance));
				cleanInviteVcoinRegistry.setVisible(true);
				cleanInviteVcoinRegistry.setSummary("端午节活动清除老活动的累积积分");
				registryService.create(cleanInviteVcoinRegistry);
			}
			
			view.addObject("loginStatus", "1");
			view.addObject("vcoinBalance", vcoinWalletService.getBalance(loginUser.getId()));
		} else {
			view.addObject("loginStatus", "0");
		}
		return view;
	}
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/dragon_boat_login_status.htm")
	public String dragonBoatLoginStatus(HttpServletRequest request){
		JSONObject json = new JSONObject();
		UserDO user = getCurrentUser(request);
		if(user == null) {
			json.put("loginStatus", 0);
		} else {
			json.put("loginStatus", 1);
		}
		return json.toJSONString();
	}
	
	@RequestMapping(value = "/dragon_boat_bill.htm")
	public ModelAndView dragonBoatBill(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dragon_boat_bill.jsp");
		UserDO loginUser = getCurrentUser(request);
		if(loginUser == null) {
			return view;
		}
		
		view.addObject("vcoinList", slaveVcoinService.findVcoinList(loginUser.getId(), "DRAGON_BOAT_FESTIVAL", 100));
		return view;
	}

}
